package com.fates.ucenter.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fates.common.lang.BlankUtils;
import com.fates.ucenter.cache.CacheService;
import com.fates.ucenter.entity.po.User;
import com.fates.ucenter.mapper.UserMapper;
import com.fates.ucenter.service.UserService;

/**
*
* User 表数据服务层接口实现类
*
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
	
	@Autowired
    private CacheService cacheService;
	
	 @Autowired
	 private SqlSessionFactory factory;
	 
	@Override
    public int setStatus(Integer userId, int status) {
        return this.baseMapper.setStatus(userId, status);
    }

    @Override
    public int changePwd(Integer userId, String pwd) {
        return this.baseMapper.changePwd(userId, pwd);
    }

    @Override
    public List<Map<String, Object>> selectUsers(String name, String beginTime, String endTime, Integer deptid) {
        return this.baseMapper.selectUsers(name, beginTime, endTime, deptid);
    }

    @Override
    public int setRoles(Integer userId, String roleIds) {
        return this.baseMapper.setRoles(userId, roleIds);
    }

    /**
     * mybatis的一级进行测试，只查询了一次
     * @param id
     * @return
     */
    @Override
    public User selectUserById(Integer userId) {
        // 自动提交事务
        SqlSession sqlSession = factory.openSession(true);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Map<String,Object> paramsMap = new HashMap<>(16);
        paramsMap.put("id",userId);
        User rmsUser = userMapper.selectOneUser(paramsMap);
        userMapper.selectOneUser(paramsMap);
        userMapper.selectOneUser(paramsMap);
        userMapper.selectOneUser(paramsMap);
        return rmsUser;
//        return cacheService.selectUserById(id);
    }
    
    @Override
    public User selectUserByAccout(String account) {
    	 return cacheService.selectUserByAccout(account);
    }

	@Override
	public void addOrUpdateUser(User user) {
		 // 添加
        if (BlankUtils.isBlank(user.getId())) {
            // 判断账号是否重复
            User theUser = selectOne(new EntityWrapper().where("account = {0}", user.getAccount()));
            if (!BlankUtils.isBlank(theUser)) {

            } else {
                insert(user);
            }
        } else { //修改
            updateById(user);
        }
	}


}
